package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.tencentUtils.TencentCloudImUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AppChatRecord;
import com.ruoyi.system.service.IAppChatRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 聊天记录Controller
 *
 * @author ruoyi
 * @date 2023-04-04
 */
@RestController
@RequestMapping("/system/record")
public class AppChatRecordController extends BaseController {
    @Autowired
    private IAppChatRecordService appChatRecordService;

    @Autowired
    private TencentCloudImUtil tencentCloudImUtil;


    /**
     * 查询聊天记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppChatRecord appChatRecord) {
        // 查询类型是否选择，聊天内容是否填充
        if (StringUtils.isNotBlank(appChatRecord.getMsgType()) && StringUtils.isBlank(appChatRecord.getMsgBody())) {
            // 拼接模糊查询
            appChatRecord.setMsgBody("\"MsgType\":" + "\"" + appChatRecord.getMsgType() + "\"");
        } else if (StringUtils.isNoneBlank(appChatRecord.getMsgType(), appChatRecord.getMsgBody())) {
            // 都填充，拼接模糊查询
            String msgBody = appChatRecord.getMsgBody();
            appChatRecord.setMsgBody("\"MsgType\":" + "\"" + appChatRecord.getMsgType() + "\"" + ",\"MsgContent\":{\"Text\":\"" + msgBody);
        }
        startPage();
        List<AppChatRecord> list = appChatRecordService.selectAppChatRecordList(appChatRecord);
        for (AppChatRecord chatRecord : list) {
            if (StringUtils.isNotEmpty(chatRecord.getMsgBody())) {
                chatRecord.setMsgBodyJson(JSONArray.parseArray(chatRecord.getMsgBody()));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出聊天记录列表
     */
    @Log(title = "聊天记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppChatRecord appChatRecord) {
        List<AppChatRecord> list = appChatRecordService.selectAppChatRecordList(appChatRecord);
        ExcelUtil<AppChatRecord> util = new ExcelUtil<AppChatRecord>(AppChatRecord.class);
        util.exportExcel(response, list, "聊天记录数据");
    }

    /**
     * 获取聊天记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(appChatRecordService.selectAppChatRecordById(id));
    }

    /**
     * 新增聊天记录
     */
    @Log(title = "聊天记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppChatRecord appChatRecord) {
        return toAjax(appChatRecordService.insertAppChatRecord(appChatRecord));
    }

    /**
     * 修改聊天记录
     */
    @Log(title = "聊天记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppChatRecord appChatRecord) {
        return toAjax(appChatRecordService.updateAppChatRecord(appChatRecord));
    }

    /**
     * 删除聊天记录
     */
    @Log(title = "聊天记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (Long id : ids) {
            AppChatRecord info = appChatRecordService.selectAppChatRecordById(id);
            if (info != null) {
                tencentCloudImUtil.adminMsgwithdraw(info.getFromAccount(), info.getToAccount(), info.getMsgKey());
            }
        }
        return toAjax(appChatRecordService.deleteAppChatRecordByIds(ids));
    }
}
